package com.smart.manager.pop.repository;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.util.ObjUtil;
import com.smart.common.entities.core.Page;
import com.smart.common.entities.enums.SmsScene;
import com.smart.manager.pop.entities.body.RDisQuestionnaire;
import com.smart.manager.pop.entities.models.PopDisQuestionnaire;
import com.smart.manager.pop.entities.models.query.QPopDisQuestionnaire;
import com.smart.manager.system.repository.ConfigRepository;
import com.smart.service.sms.SmsService;
import io.ebean.DB;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 支付中心-订单管理
 *
 * @author Administrator
 */

@Log4j2
@Service
@RequiredArgsConstructor
public class DisQuestionnaireRepository {

    private final SmsService smsService;
    private final ConfigRepository configRepository;

    public Page<PopDisQuestionnaire> pageList(RDisQuestionnaire.SearchBody search) {

        QPopDisQuestionnaire query = new QPopDisQuestionnaire();
        Opt.ofBlankAble(search.getName()).ifPresent(x -> query.name.contains(x));
        query.setFirstRow(search.firstRow()).setMaxRows(search.maxRows()).createTime.desc();
        return new Page<PopDisQuestionnaire>().load(query.findPagedList());
    }

    /**
     * 添加
     *
     * @param form 形式
     */
    public String add(RDisQuestionnaire.EditBody form) {
        //验证手机号
        smsService.checkCaptchaCode(SmsScene.MEMBER_REGISTER, form.getMobilePhone(), form.getCode());
        //通过处理
        PopDisQuestionnaire record = BeanUtil.copyProperties(form, PopDisQuestionnaire.class);
        record.save();
        return String.valueOf(record.getId());
    }

    public String addSubstance(Long id, RDisQuestionnaire.EditBody2 form) {
        log.warn(id);
        log.warn(form);
        PopDisQuestionnaire one = new QPopDisQuestionnaire().id.eq(id).setMaxRows(1).findOne();
        if (ObjUtil.isNotNull(one)) {
            one.setSubstanceList(form.getSubstanceList()).update();
        }
        return String.valueOf(one.getShareId());
    }


    public void getCaptcha(RDisQuestionnaire.EditBody3 form) {
        smsService.sendCaptchaCode(SmsScene.MEMBER_REGISTER, form.getMobilePhone());
    }

    /**
     * 更新
     *
     * @param id   id
     * @param form 形式
     */
    public void update(Long id, RDisQuestionnaire.EditBody form) {
        PopDisQuestionnaire record = new QPopDisQuestionnaire().id.eq(id).findOne();
        Assert.notNull(record, "等级规则数据不存在");
        BeanUtil.copyProperties(form, record);
        record.update();


    }

    /**
     * 批量删除
     *
     * @param idList id列表
     * @return int
     */
    public int delByIds(List<Long> idList) {
//        long count = new QPopDisQuestionnaire().id.in(idList).findStream().filter(PopDisQuestionnaire::locked).count();
//        Assert.isTrue(count == 0, "操作失败:存在已有数据的客户经理账号");
        return DB.deleteAll(PopDisQuestionnaire.class, idList);
    }


}
